﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace BACKOFFICE
{
    public partial class SeatingChart : DevExpress.XtraEditors.XtraUserControl
    {
        private Control activeControl;
        private Point previousLocation;
        private Control PropertiesControl;
        private Control PreviousPropertiesControl;

        int HabachiBtnNumber = 1;
        int DinningBtnNumber = 1;
        int PrivateBtnNumber = 1;
        int BarBtnNumber = 1;
        int SushiBarBtnNumber = 1;

        public SeatingChart()
        {
            InitializeComponent();

            SimpleButton btnOpenTicket = new SimpleButton();
            btnOpenTicket.Name = "btnOpenTicket";
            btnOpenTicket.Text = "OPEN TICKETS";
            btnOpenTicket.Width = 75;
            btnOpenTicket.Height = 75;
            btnOpenTicket.Location = new Point(5, 5);
            btnOpenTicket.Appearance.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
            btnOpenTicket.MouseDown += new MouseEventHandler(btn_MouseDown);
            btnOpenTicket.MouseMove += new MouseEventHandler(btn_MouseMove);
            btnOpenTicket.MouseUp += new MouseEventHandler(btn_MouseUp);
            panelControl1.Controls.Add(btnOpenTicket);
        }

        private void Testing_Load(object sender, EventArgs e)
        {

        }

        void btn_MouseDown(object sender, MouseEventArgs e)
        {
            activeControl = sender as Control;
            previousLocation = e.Location;
            Cursor = Cursors.Hand;
        }

        void btn_MouseMove(object sender, MouseEventArgs e)
        {
            if (activeControl == null || activeControl != sender)
                return;

            var location = activeControl.Location;
            location.Offset(e.Location.X - previousLocation.X, e.Location.Y - previousLocation.Y);
            activeControl.Location = location;
        }

        void btn_MouseUp(object sender, MouseEventArgs e)
        {

            PropertiesControl = activeControl;
            UpdatePropertiesInfo();
            activeControl = null;
            Cursor = Cursors.Default;
        }

        void UpdatePropertiesInfo()
        {
            TxtTitle.Text = PropertiesControl.Text;
            TxtLocationX.Text = PropertiesControl.Location.X.ToString();
            txtLocationY.Text = PropertiesControl.Location.Y.ToString();
            txtWidthX.Text = PropertiesControl.Width.ToString();
            txtHeightY.Text = PropertiesControl.Height.ToString();
        }

        private void btnHabachiTable_Click(object sender, EventArgs e)
        {
            var btnHabachi = new SimpleButton();
            btnHabachi.Width = 70;
            btnHabachi.Height = 70;
            btnHabachi.Name = "btnHabachi" + HabachiBtnNumber.ToString();
            btnHabachi.Text = "Habachi " + HabachiBtnNumber.ToString();
            btnHabachi.Location = new Point(100, 100);
            btnHabachi.MouseDown += new MouseEventHandler(btn_MouseDown);
            btnHabachi.MouseMove += new MouseEventHandler(btn_MouseMove);
            btnHabachi.MouseUp += new MouseEventHandler(btn_MouseUp);

            panelControl1.Controls.Add(btnHabachi);
            HabachiBtnNumber++;

            PreviousPropertiesControl = PropertiesControl;
            PropertiesControl = btnHabachi;
            UpdatePropertiesInfo();
        }

        private void btnDinningSeat_Click(object sender, EventArgs e)
        {
            var btnDinning = new SimpleButton();
            btnDinning.Width = 70;
            btnDinning.Height = 70;
            btnDinning.Text = "Dinning " + DinningBtnNumber.ToString();
            btnDinning.Location = new Point(100, 100);
            btnDinning.MouseDown += new MouseEventHandler(btn_MouseDown);
            btnDinning.MouseMove += new MouseEventHandler(btn_MouseMove);
            btnDinning.MouseUp += new MouseEventHandler(btn_MouseUp);

            panelControl1.Controls.Add(btnDinning);
            DinningBtnNumber++;

            PreviousPropertiesControl = PropertiesControl;
            PropertiesControl = btnDinning;
            UpdatePropertiesInfo();
        }

        private void btnPrivateRoom_Click(object sender, EventArgs e)
        {
            var btnPrivate = new SimpleButton();
            btnPrivate.Width = 70;
            btnPrivate.Height = 70;
            btnPrivate.Text = "Private " + PrivateBtnNumber.ToString();
            btnPrivate.Location = new Point(100, 100);
            btnPrivate.MouseDown += new MouseEventHandler(btn_MouseDown);
            btnPrivate.MouseMove += new MouseEventHandler(btn_MouseMove);
            btnPrivate.MouseUp += new MouseEventHandler(btn_MouseUp);

            panelControl1.Controls.Add(btnPrivate);
            PrivateBtnNumber++;

            PreviousPropertiesControl = PropertiesControl;
            PropertiesControl = btnPrivate;
            UpdatePropertiesInfo();
        }

        private void btnBarSeat_Click(object sender, EventArgs e)
        {
            var btnBarSeat = new SimpleButton();
            btnBarSeat.Width = 50;
            btnBarSeat.Height = 50;
            btnBarSeat.Text = "Bar " + BarBtnNumber.ToString();
            btnBarSeat.Location = new Point(100, 100);
            btnBarSeat.MouseDown += new MouseEventHandler(btn_MouseDown);
            btnBarSeat.MouseMove += new MouseEventHandler(btn_MouseMove);
            btnBarSeat.MouseUp += new MouseEventHandler(btn_MouseUp);

            panelControl1.Controls.Add(btnBarSeat);
            BarBtnNumber++;
            //if (PreviousPropertiesControl != null)
            //{
            PreviousPropertiesControl = PropertiesControl;
            PropertiesControl = btnBarSeat;
            UpdatePropertiesInfo();
            //}
        }

        private void btnSushiBarSeat_Click(object sender, EventArgs e)
        {
            var btnSushiBarSeat = new SimpleButton();
            btnSushiBarSeat.Width = 50;
            btnSushiBarSeat.Height = 50;
            btnSushiBarSeat.Text = "Sushi " + SushiBarBtnNumber.ToString();
            btnSushiBarSeat.Location = new Point(100, 100);
            btnSushiBarSeat.MouseDown += new MouseEventHandler(btn_MouseDown);
            btnSushiBarSeat.MouseMove += new MouseEventHandler(btn_MouseMove);
            btnSushiBarSeat.MouseUp += new MouseEventHandler(btn_MouseUp);

            panelControl1.Controls.Add(btnSushiBarSeat);
            SushiBarBtnNumber++;

            PreviousPropertiesControl = PropertiesControl;
            PropertiesControl = btnSushiBarSeat;
            UpdatePropertiesInfo();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            SaveLoadXMLSeattingChart.buttons.Clear();

            foreach (Control controls in panelControl1.Controls)
            {
                SaveLoadXMLSeattingChart.buttons.Add(controls);
            }

            SaveLoadXMLSeattingChart.WriteToXml();
        }

        private void TxtTitle_Leave(object sender, EventArgs e)
        {
            PropertiesControl.Text = TxtTitle.Text;
        }
    }
}
